extends Node

var CARD_INTRODUCE: Resource = preload("res://src/battle/card/card_info/card_introduce.csv")
var CARD_INTRODUCE_DIR: Dictionary = {}

var STATE_INTRODUCE: Resource = preload("res://src/battle/state/state_introduce.csv")
var STATE_INTRODUCE_DIR: Dictionary = {}

var ALL_STATE_TRES_DICT = preload("res://src/battle/state/all_state_tres_dict.tres")
var ALL_PROP_TRES_DICT = preload("res://src/battle/prop/all_prop_tres_dict.tres")

#职业类型0战士
var player_type: AllPlayerTypeMap.PLAYER_TYPE = AllPlayerTypeMap.PLAYER_TYPE.WARRIOR

var main_secne

var current_monster: Monster
var monster_handler
var player_in_battle
var player_state: PlayerInBattleState
#破甲比率
var armor_break_per = 1.3
#虚弱比率
var weak_per = 0.75
var current_room
var init_room


func _ready():
	ALL_STATE_TRES_DICT.load_state()
	ALL_PROP_TRES_DICT.load_prop()
	TranslationServer.set_locale("zh")
	for introduce in CARD_INTRODUCE.records:
		CARD_INTRODUCE_DIR[introduce[0]] = introduce
	for introduce in STATE_INTRODUCE.records:
		STATE_INTRODUCE_DIR[introduce[0]] = introduce
	Events.take_damage.connect(_on_take_damage)


func _on_take_damage(from, to, damage):
	if to == null:
		return
	Shake.show_number(to, damage)
	to.take_damage(damage)
	if from != null and from.state is PlayerInBattleState:
		from.state.calcute_anger(damage)
		Events.card_pool_info_update.emit()
		Events.card_table_card_info_update.emit()
	if to.state.counter_attack != 0:
		from.take_damage(to.state.counter_attack)
		Shake.show_number(from, to.state.counter_attack)
		Shake.shake(from, 16, 0.5)

func trial_take_damage(to, damage, times = 1):
	if to == null:
		return -1
	return to.trial_take_damage(damage, times)

		
func get_final_damage(from_state, to_state, damage, anger_addition_damage = 0) -> int:
	var final_damage = damage + anger_addition_damage
	if from_state == null:
		return final_damage
	if from_state.has_weak_state():
		final_damage = floori(final_damage * Globals.weak_per)
	if to_state != null and to_state.has_armor_brak_state():
		final_damage = ceili(final_damage * Globals.armor_break_per)
	if to_state != null and to_state.has_unyielding_state():
		final_damage = ceili(final_damage * 0.5)
	return final_damage


func get_final_shield(from_state, shield, anger_addition_shield = 0) -> int:
	var final_shield = shield + anger_addition_shield
	if from_state == null:
		return final_shield
	if from_state.has_weak_state():
		final_shield = floori(final_shield * Globals.weak_per)
	return final_shield
